
clear all


set more off

scalar def lowlim		=0.85
scalar def highlim		=5

cd "$revdta"

**********************************************************************************************
*
use "Full_Panel_2b.dta", clear
keep if sample_analytic==1  & educ <= 2

*bysort newid: egen everSSI=max(tr_ssi >0 & tr_ssi != . & DI ==0)
*bysort newid: egen everboth=max(tr_ssi >0 & DI==1)
*drop if everSSI ==1 
*drop everSSI

gen emp = hours >=1500 & hours != .
*bysort newid: egen everworkhealthy=max(emp == 1 & DS == 0)
*keep if everworkhealthy == 1 & everSSI == 0
*drop everworkhealthy


cd "$out"

drop age2
gen sp_dis = sp_DS==1 | sp_DS==2
gen retired = empst == 4
gen part_full = lw!=. & hours >=1500 
gen part_part = lw !=. & hours < 1500 & hours >=500

gen sp_part_full = sp_lw !=. & sp_hours >=1500
gen sp_part_part = sp_lw != . & sp_hours < 1500 & sp_hours >=500

gen 	age2=age^2
replace age2=age2/100
/*
/*Probably food outliers*/
replace food=. if food>1199987		
replace fout=. if food>129999
egen totfood=rsum(food fout)
*/
*replace rent=0.06*house if rent==0 & house>0
/*
egen spending=rsum(totfood health educexp util childcare transport rent homeins)
replace spending=spending/price
*/
gen lc=ln(spending)

replace lc = . if year<=1996
keep if year > 1996
tempfile temp
save `temp',replace

**********************************************************************************************
u `temp',clear

label var age "Age of Reference Person"

gen single = married == 0
*keep if married==1
g logy=ln(ly) 
*if hours >= 1500
xtile vc=lc,nq(1000)
*replace lc=. if vc<=25|vc>=975
xtile vy=logy,nq(1000)
*replace logy=. if vy<=25|vy>=975

g logwy=ln(wly) 
*if  sp_hours >=1500 
xtile wvy=logwy,nq(1000)
*replace logwy=. if wvy<=25|wvy>=975

g logtotinc=ln(y) 
xtile vtotinc=logtotinc,nq(1000)
*replace logtotinc=. if vtotinc<=25|vtotinc>=975

gen lc1 = lc if sev!=1 & mod !=1
gen lc2 = lc if sev!=1 & mod ==1
gen lc3 = lc if sev==1 & mod !=1

preserve 
collapse (mean) lc* logy* logwy* wly ly (median) logtotinc,by(age)
sort age
*merge age using consdetails
*drop _merge
*sort age
*merge age using wagedetails
*(drop _merge
*sort age

cd $mout

merge 1:1 age using "modelWage_spouse_$modeltype.dta"

drop _merge
rename V1 mywage
label var mywage "My Sim"

merge 1:1 age using "modelEarnings_spouse_$modeltype.dta"

drop _merge
rename V1 myearnings
label var myearnings "Simulation"


merge 1:1 age using "modelspWage_spouse_$modeltype.dta"

drop _merge
rename V1 myspwage
label var myspwage "Simulation"

merge 1:1 age using "modelspEarnings_spouse_$modeltype.dta"

drop _merge
rename V1 myspearnings
label var myspearnings "Simulation"


lab var logy "Data"
*lab var logpay "L&P Sim"
scatter mywage logy age,ms(O oh) xlabel(20(5)65) graphregion(color(white)) saving(inc,replace) title(Log(earnings))
graph export "fig_wage_spouse_$modeltype.pdf", replace

lab var ly "Data"
scatter myearnings ly age,ms(O oh) xlabel(20(5)65) graphregion(color(white)) saving(spinc,replace) title(Income)
graph export "fig_earn_spouse_$modeltype.pdf", replace

lab var logwy "Data"
scatter myspwage logwy age,ms(O oh) xlabel(20(5)65) graphregion(color(white)) saving(spinc,replace) title(Log(earnings))
graph export "fig_spwage_spouse_$modeltype.pdf", replace

lab var wly "Data"
scatter myspearnings wly age,ms(O oh) xlabel(20(5)65) graphregion(color(white)) saving(spinc,replace) title(Spousal Income)
graph export "fig_spearn_spouse_$modeltype.pdf", replace

*su logpay
*replace logpay=logpay-r(mean)
su mywage
replace mywage=mywage-r(mean)
su logy 
replace logy=logy-r(mean)
scatter mywage logy age,ms(O oh) ylabel(-0.4(0.1)0.2) xlabel(20(5)65) graphregion(color(white)) saving(inc,replace) title(Log(Ref. Person Earnings))
graph export "fig_wage_demean_spouse_$modeltype.pdf", replace

gen logmyearn = log(myearnings)
gen logymean =log(ly)
su logymean
replace logymean=logymean-r(mean)
su logmyearn
replace logmyearn=logmyearn-r(mean)
label var logmyearn "Sim"
label var logymean "Data"
scatter logmyearn logymean age,ms(O oh) ylabel(-0.8(0.2)0.2) xlabel(20(5)65) graphregion(color(white))  ytitle(Log of Average Earnings)
graph export "fig_earn_demean_spouse_$modeltype.pdf", replace

su logwy
replace logwy=logwy-r(mean)
su myspwage
replace myspwage=myspwage-r(mean)
scatter logwy myspwage age,ms(O oh) ylabel(-0.4(0.1)0.2) xlabel(20(5)65) graphregion(color(white)) saving(inc,replace) title(Log(Partner Earnings))
graph export "fig_spwage_demean_spouse_$modeltype.pdf", replace


restore
preserve
merge m:1 newid using "../intermediary/sp_temp_types"
keep if _merge == 3
if("$type" != "type_prodwage") ren type_prodwage type_prod
ren $type Type
drop _merge

gen 	scale=1+0.5*(fsize-kids-1)+0.3*kids		
gen lc_eq = lc - log(scale)

collapse (mean) lc* logy* logwy* wly ly (median) logtotinc,by(age Type)
lab var lc "Data"

merge 1:1 age Type using "modelSpending_bytype$modeltype.dta"
drop _merge


rename Ceq mycons
label var mycons "Simulation"
rename C mycons_raw
label var mycons_raw "Simulation"

keep if age <= 62

su lc
replace lc=lc-r(mean)
su mycons_raw if age <=62
replace mycons_raw=mycons_raw-r(mean) if age <= 62

su lc_eq
replace lc_eq = lc_eq - r(mean)
su mycons if age <= 62
replace mycons = mycons - r(mean) if age <= 62

twoway (scatter mycons_raw  age if Type== 1 & age <=62, ms(O) msize(vlarge vlarge)) (scatter mycons_raw   age if Type == 2 & age <=62, ms(D) msize(vlarge vlarge)) (scatter mycons_raw   age if Type == 3 & age <=62, ms(T) msize(vlarge vlarge)),xlabel(20(10)60 ,labsize(large)) graphregion(color(white)) saving(cons_sim,replace) ytitle(Log(consumption), size(large)) legend(label(1 "Low Type") label(2 "Mid Type") label(3 "High Type") rows(1)) title("Simulation",position(6))  yscale(off) fxsize(60)
*logcon

twoway (scatter lc   age if Type== 1 & age <=62, ms(O) msize(vlarge vlarge)) (scatter lc   age if Type == 2 & age <=62, ms(D) msize(vlarge vlarge)) (scatter lc   age if Type == 3 & age <=62, ms(T) msize(vlarge vlarge)),xlabel(20(10)60 ,labsize(large)) graphregion(color(white)) saving(cons_data,replace) ytitle(Log(consumption), size(large)) legend(label(1 "Low Type") label(2 "Mid Type") label(3 "High Type") rows(1)) title("Data",position(6)) fxsize(65)

grc1leg cons_data.gph cons_sim.gph,graphregion(color(white))

graph export "fig_cons_demean_bytype_$modeltype.pdf", replace

twoway (scatter mycons age if Type== 1 & age <=62, ms(O) msize(vlarge vlarge)) (scatter mycons age if Type == 2 & age <=62, ms(D) msize(vlarge vlarge)) (scatter mycons age if Type == 3 & age <=62, ms(T) msize(vlarge vlarge)),xlabel(20(10)60 ,labsize(large)) graphregion(color(white)) saving(cons_sim,replace) ytitle(Log(consumption), size(large)) legend(label(1 "Low Type") label(2 "Mid Type") label(3 "High Type") rows(1)) title("Simulation",position(6))  yscale(off) fxsize(60)
*logcon

twoway (scatter lc_eq   age if Type== 1 & age <=62, ms(O) msize(vlarge vlarge)) (scatter lc_eq   age if Type == 2 & age <=62, ms(D) msize(vlarge vlarge)) (scatter lc_eq   age if Type == 3 & age <=62, ms(T) msize(vlarge vlarge)),xlabel(20(10)60 ,labsize(large)) graphregion(color(white)) saving(cons_data,replace) ytitle(Log(consumption), size(large)) legend(label(1 "Low Type") label(2 "Mid Type") label(3 "High Type") rows(1)) title("Data",position(6)) fxsize(65)

grc1leg cons_data.gph cons_sim.gph,graphregion(color(white))

graph export "fig_cons_eq_demean_bytype_$modeltype.pdf", replace

restore
collapse (mean) lc* logy* logwy* wly ly (median) logtotinc,by(age single)


lab var lc "Data"

merge 1:1 age single using "modelSpending_spouse$modeltype.dta"

drop _merge

rename Ceq mycons
label var mycons "Simulation"
rename C mycons_raw
label var mycons_raw "Simulation"

 
merge 1:1 age single using "modelGrossIncome_spouse_$modeltype.dta"
ren V1 mytotinc
label var mytotinc "Simulation"

keep if age <= 62
*su logcon 
*replace logcon=logcon-r(mean)
su lc 
*replace lc=lc-r(mean)
*scatter lc logcon mycons_raw age,ms(O oh) xlabel(20(5)65) graphregion(color(white)) saving(cons,replace) title(Log(spending))
scatter mycons_raw lc  age,ms(O oh) xlabel(20(5)65) graphregion(color(white)) saving(cons,replace) title(Log(consumption), size(large))
graph export "fig_cons_spouse_$modeltype.pdf", replace

*su logcon 
*replace logcon=logcon-r(mean)
su lc
replace lc=lc-r(mean)
su mycons_raw if age <=62
replace mycons_raw=mycons_raw-r(mean) if age <= 62
twoway (scatter mycons_raw  lc   age if single == 0 & age <=62, ms(O oh) msize(vlarge vlarge)) (scatter mycons_raw  lc   age if single == 1 & age <=62, ms(T th) msize(vlarge vlarge)),xlabel(20(10)60 ,labsize(large)) graphregion(color(white)) saving(cons,replace) ytitle(Log(consumption), size(large)) legend(label(1 "Simulation (married)") label(2 "Data (married)") label(3 "Simulation (unmarried)") label(4 "Data (unmarried)"))
*logcon
graph export "fig_cons_demean_spouse_$modeltype.pdf", replace

*scatter cons1 cons2 cons3 lc1 lc2 lc3 age,ms(O oh) xlabel(20(5)65) graphregion(color(white)) saving(cons,replace) title(Log(spending))



label var logtotinc "Data"
su logtotinc
replace logtotinc = logtotinc - r(mean)
su mytotinc
replace mytotinc = mytotinc - r(mean)
twoway (scatter mytotinc  logtotinc  age if single == 0 & age <=62, ms(O oh)  msize(vlarge vlarge)) (scatter mytotinc  logtotinc  age if single == 1 & age <=62, ms(T th)  msize(vlarge vlarge)),xlabel(20(10)60 ,labsize(large)) graphregion(color(white)) saving(inc,replace) ytitle(Log(Household Income), size(large)) legend(label(1 "Simulation (married)") label(2 "Data (married)") label(3 "Simulation (unmarried)") label(4 "Data (unmarried)"))
graph export "fig_totinc_demean_spouse_$modeltype.pdf", replace

**********************************************************************************************

grc1leg  cons.gph inc.gph,graphregion(color(white))
graph export "fig_consinc_spouse_$modeltype.pdf", replace

erase inc.gph
erase cons.gph 

clear
